package Teach;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * description:判断两个文件是否相同
 * author:zcx
 * lenovo
 * 时间：2022.01.18.15.58
 */
public class Pj9 {
    public static void main(String[] args) {
       var src=new File("my.java");
       var dst=new File("my.txt");
        if (isSame(src, dst)) {
            System.out.println("是同一个文件");
        }else {
            System.out.println("不是同一个文件");
        }
    }

    public  static boolean isSame1(File src,File dst){
        boolean f=false;
        if(src.length()==dst.length()){
            try(var f1=new FileInputStream(src);var f2=new FileInputStream(dst)) {
                long temp=0;
                while (f1.read()==f2.read()){

                    ++temp;
                    if(temp==src.length()){
                        break;
                    }
                }
                if(temp==src.length()){
                    f=true;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }


    public  static boolean isSame(File src,File dst){
        boolean f=false;
        if(src.length()==dst.length()){
            try(var f1=new FileInputStream(src);var f2=new FileInputStream(dst)) {
                long temp=0;
                while (f1.read()==f2.read()){

                    ++temp;
                    if(temp==src.length()){
                        break;
                    }
                }
                if(temp==src.length()){
                    f=true;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;

    }
}
